package com.niit.search.controller;

import com.niit.chat.common.Result;
import com.niit.search.entity.Activity;
import com.niit.search.entity.Topic;
import com.niit.search.entity.User;
import com.niit.search.service.ActivityService;
import com.niit.search.service.SearchHistoryService;
import com.niit.search.service.TopicService;
import com.niit.search.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private UserService userService;
    @Autowired
    private SearchHistoryService searchHistoryService;
    @Autowired
    private TopicService topicService;

    @GetMapping
    public Result<?> search(@RequestParam String type, @RequestParam String keyword) {
        List<Map<String, Object>> result = new ArrayList<>();
        if ("all".equals(type) || "activity".equals(type)) {
            List<Activity> activities = activityService.searchActivity(keyword);
            for (Activity a : activities) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", a.getId());
                map.put("name", a.getContent());
                map.put("type", "activity");
                result.add(map);
            }
        }
        if ("all".equals(type) || "user".equals(type)) {
            List<User> users = userService.searchUserByUsername(keyword);
            for (User u : users) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", u.getId());
                map.put("name", u.getNickname() != null ? u.getNickname() : u.getUsername());
                map.put("username", u.getUsername());
                map.put("type", "user");
                result.add(map);
            }
        }
        if ("all".equals(type) || "nickname".equals(type)) {
            List<User> users = userService.searchUser(keyword);
            for (User u : users) {
                if (u.getNickname() != null && u.getNickname().contains(keyword)) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", u.getId());
                    map.put("name", u.getNickname());
                    map.put("username", u.getUsername());
                    map.put("type", "nickname");
                    result.add(map);
                }
            }
        }
        if ("all".equals(type) || "topic".equals(type)) {
            try {
                List<Topic> topics = topicService.searchTopics(keyword);
                for (Topic t : topics) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", t.getId());
                    map.put("name", t.getContent());
                    map.put("type", "topic");
                    result.add(map);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return Result.success(result);
    }

    @GetMapping("/user/{id}")
    public Result<User> getUserById(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }
}